<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="480px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-left: 55px"
      >
        <!-- <el-col :span="24">
          <el-form-item label="父项物料代码" prop="itemWldm" label-width="100px">
            <el-input
              v-model="queryParams.itemWldm"
              placeholder="请输入父项物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="物料代码" prop="itemZxWldm" label-width="100px">
            <el-input
              v-model="queryParams.itemZxWldm"
              placeholder="请输入子项物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:itemMatch:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:itemMatch:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:itemMatch:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:itemMatch:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :height="tableH"
      border
      stripe
      :row-style="{ padding: '0px' }"
      :cell-style="{ padding: '0px 0' }"
      :header-row-style="{ padding: '2px 0' }"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="itemMatchList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="父项物料代码" width="130" show-overflow-tooltip align="center" prop="itemWldm" />
      <el-table-column label="品名规格" width="350" show-overflow-tooltip prop="itemPmgg" /> -->
      <el-table-column
        label="子项物料代码"
        width="130"
        show-overflow-tooltip
        align="center"
        prop="itemZxWldm"
      />
      <el-table-column
        label="品名规格"
        width="375"
        show-overflow-tooltip
        prop="itemZxPmgg"
      />
      <el-table-column
        label="变更类型"
        width="320"
        show-overflow-tooltip
        prop="cgStatus"
      >
        <template #default="scope">
          <el-radio-group
            v-model="scope.row.cgStatus"
            size="mini"
            @change="
              (v) => {
                handleUpdate(scope.row, v);
              }
            "
          >
            <el-radio
              v-for="dict in $store.state.dicts.item_match_cg_status"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >
              {{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column
        label="变更后采购属性"
        width="150"
        show-overflow-tooltip
        prop="cgMstrId"
        align="center"
      >
        <template #default="scope">
          <el-select
            v-if="scope.row.cgStatus == 2"
            v-model="scope.row.cgMstrId"
            placeholder="请选择采购属性"
            size="mini"
            disabled
          >
            <el-option
              v-for="dict in bomCgsxOptionsNromal"
              :key="dict.cgId"
              :label="dict.cgName"
              :value="dict.cgId"
            />
          </el-select>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        align="center"
        prop="itemDesc"
      />
      <el-table-column
        label="建立人员"
        width="92"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        align="center"
        prop="createTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        width="92"
        align="center"
        prop="updateBy"
      />
      <el-table-column
        label="更改日期"
        align="center"
        prop="updateTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改物料采购属性变更对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="父项物料代码" prop="itemWldm" label-width="120px">
          <el-autocomplete
            class="inline-input"
            v-model="form.itemWldm"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            :trigger-on-focus="false"
            @select="handleSelect"
            :disabled="title === '修改物料采购属性变更'"></el-autocomplete>
        </el-form-item> -->
        <el-form-item
          label="子项物料代码"
          prop="itemZxWldm"
          label-width="120px"
        >
          <el-autocomplete
            class="inline-input"
            v-model="form.itemZxWldm"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            :trigger-on-focus="false"
            @select="handleSelect"
            :disabled="title === '修改物料采购属性变更'"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="变更类型" prop="cgStatus" label-width="120px">
          <el-radio-group v-model="form.cgStatus">
            <el-radio
              v-for="dict in $store.state.dicts.item_match_cg_status"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >
              {{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="变更后采购属性"
          prop="cgMstrId"
          label-width="120px"
          v-show="form.cgStatus == 2"
        >
          <el-select v-model="form.cgMstrId" placeholder="请选择采购属性">
            <el-option
              v-for="dict in bomCgsxOptionsNromal"
              :key="dict.cgId"
              :label="dict.cgName"
              :value="dict.cgId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="匹配采购描述" prop="itemDesc" label-width="120px">
          <el-input
            v-model="form.itemDesc"
            placeholder="请输入匹配采购属性描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItemMatch,
  getItemMatch,
  delItemMatch,
  addItemMatch,
  updateItemMatch,
  exportItemMatch,
  selectWldm,
} from "@/api/project/itemMatch";
import { getAllCgList, getAllCgListNormal } from "@/api/project/mstr";
export default {
  name: "ItemMatch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料采购属性变更表格数据
      itemMatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemWldm: null,
        itemZxWldm: null,
      },
      // 表单参数
      form: {},
      bomCgsxOptionsNromal: [],
    };
  },
  computed: {
    // 表单校验
    rules() {
      return {
        cgStatus: [
          { required: true, message: "请选择变更状态", trigger: "change" },
        ],
        cgMstrId: [
          {
            required: this.form.cgStatus == 2,
            message: "请选择变更后采购属性",
            trigger: "change",
          },
        ],
      };
    },
  },
  created() {
    this.$store.dispatch("GetDict", "item_match_cg_status");
    getAllCgListNormal().then((response) => {
      for (let item of response.data) {
        if (item.cgId === 4 || item.cgId === 5 || item.cgId === 7) {
          this.bomCgsxOptionsNromal.push(item);
        }
      }
    });
    this.getList();
  },
  methods: {
    /** 查询物料采购属性变更列表 */
    getList() {
      this.loading = true;
      this.tableH = document.body.clientHeight - 280 > 600 ? 657 : 400;

      this.queryParams.pageSize = this.tableH > 600 ? 25 : 15;
      listItemMatch(this.queryParams).then((response) => {
        this.itemMatchList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        itemWldm: null,
        itemDesc: null,
        itemZxWldm: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    querySearch(queryString, cb) {
      selectWldm(queryString).then((response) => {
        this.dyWldm = response.data;
        // this.loading = false;
        this.restaurants = this.dyWldm;
        for (let item of this.restaurants) {
          item.value = item.itmWldm;
        }

        // let restaurants = this.restaurants;
        // let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(this.restaurants);
        // console.log(this.restaurants)
      });
    },
    //父项
    handleSelect(item) {
      console.log(item);
      this.form.dylFxdmmc = item.itmPmgg;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.itemId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料采购属性变更";
    },
    handleSearch() {
      this.reset();
      this.title = "查询机台特性";
      this.openSearch = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, v) {
      this.reset();
      const itemId = row.itemId || this.ids;
      getItemMatch(itemId).then((response) => {
        this.form = response.data;
        if (v) this.form.cgStatus = v;
        this.open = true;
        this.title = "修改物料采购属性变更";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.itemId != null) {
            updateItemMatch(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItemMatch(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$confirm("是否确认删除选择的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delItemMatch(itemIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有物料采购属性变更数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportItemMatch(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
